//
//  SampleHandler.m
//  boardExtenion
//
//  Created by 3tee on 2022/8/18.
//  Copyright © 2022年 3tee. All rights reserved.
//


#import "SampleHandler.h"
#import <avd_sdk/AVDSampleHandlerSocketManager.h>
#define frameTime 1000/15

@implementation SampleHandler

- (void)broadcastStartedWithSetupInfo:(NSDictionary<NSString *,NSObject *> *)setupInfo {
    // User has requested to start the broadcast. Setup info from the UI extension can be supplied but optional.
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter (), (__bridge const void *)(self), broadcastStopCallback,CFSTR("broadcastStop"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fishionBroadNotification:) name:@"FISHIONBROADCASTNOTIFICATION" object:nil];
    [self sendNotificationForMessageWithIdentifier:@"broadcastStartedWithSetupInfo" userInfo:nil];
    [[AVDSampleHandlerSocketManager sharedManager] setUpSocket];
}
void broadcastStopCallback (CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo){
    [[NSNotificationCenter defaultCenter] postNotificationName:@"FISHIONBROADCASTNOTIFICATION" object:nil userInfo:nil];
}

- (void)broadcastPaused {
    // User has requested to pause the broadcast. Samples will stop being delivered.
    
}
- (void)broadcastResumed {
    // User has requested to resume the broadcast. Samples delivery will resume.
}

- (void)broadcastFinished {
    // User has requested to finish the broadcast.
    [[AVDSampleHandlerSocketManager sharedManager] socketDelloc];
    [self sendNotificationForMessageWithIdentifier:@"broadcastFinished" userInfo:nil];
    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(),
                                       (__bridge const void *)(self),
                                       CFSTR("broadcastUnpublish"),
                                       NULL);
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"FISHIONBROADCASTNOTIFICATION" object:nil];
}

- (void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer withType:(RPSampleBufferType)sampleBufferType {
    switch (sampleBufferType) {
        case RPSampleBufferTypeVideo:{
            // Handle video sample buffer
            [[AVDSampleHandlerSocketManager sharedManager] sendVideoBufferToHostApp2:sampleBuffer];
        }
            break;
        case RPSampleBufferTypeAudioApp:
            // Handle audio sample buffer for app audio
            break;
        case RPSampleBufferTypeAudioMic:
            // Handle audio sample buffer for mic audio
            break;
        default:
            break;
    }
}

- (void)sendNotificationForMessageWithIdentifier:(nullable NSString *)identifier userInfo:(NSDictionary *)info {
    CFNotificationCenterRef const center = CFNotificationCenterGetDarwinNotifyCenter();
    CFDictionaryRef userInfo = (__bridge CFDictionaryRef)info;
    BOOL const deliverImmediately = YES;
    CFStringRef identifierRef = (__bridge CFStringRef)identifier;
    CFNotificationCenterPostNotification(center, identifierRef, NULL, userInfo, deliverImmediately);
}

- (void)fishionBroadNotification:(NSNotification *)noti{
    [[AVDSampleHandlerSocketManager sharedManager] socketDelloc];
    NSError *err = [NSError errorWithDomain:@"屏幕直播已停止" code:-10000 userInfo:@{NSLocalizedDescriptionKey:@"停止直播"}];
    [self finishBroadcastWithError:err];
}

- (void)finishBroadcastWithError:(NSError *)error{
    [super finishBroadcastWithError:error];
    if (error.code != -10000) {
        [self sendNotificationForMessageWithIdentifier:@"broadcastFinishedError" userInfo:nil];
    }
}

- (void)beginRequestWithExtensionContext:(NSExtensionContext *)context{
    [super beginRequestWithExtensionContext:context];
    
}
@end
